package com.fc.redap.helper;

import java.util.Random;

public class RandomUtils {
    private static String DEFAULT_MIXED_SOURCE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static String DEFAULT_NUMBER_SOURCE = "0123456789";
    private static String DEFAULT_ALPHABET_SOURCE = "abcdefghijklmnopqrstuvwxyz";

    public static String generateRandomCode(int verifySize, String sources){
        if(sources == null || sources.length() == 0){
            sources = DEFAULT_MIXED_SOURCE;
        }
        int codesLen = sources.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(verifySize);
        for(int i = 0; i < verifySize; i++){
            verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
        }
        return verifyCode.toString();
    }

    public static String generateRandomNumber(int verifySize){
        return generateRandomCode(verifySize, DEFAULT_NUMBER_SOURCE);
    }

    public static String generateRandomAlphabet(int verifySize){
        return generateRandomCode(verifySize, DEFAULT_ALPHABET_SOURCE);
    }
}
